<?php namespace sacvoyage\hook;

use \sacvoyage\hook\Hook;
use \sacvoyage\Object;
use \sacvoyage\domain\Collection;

class AccountHook extends Hook {
	
	public function onWrite(Collection $collection, $documents) {
		if(!is_enumerative_array($documents) && !$documents instanceof \Iterator){
			$documents=array($documents);
		}
		// user writes accunt-config to own sacvoyage
		if(self::$catch_user_write && $collection->getName()=='sys.cfg') {
			self::$catch_root_write=false;
			
			$owner=$collection->getOwner();
			$root=$owner->getDomain()->loadUser('root');
			$allShares=$root->getProperty()->getCollection('sys.user.accounts');
			
			// just write the same document to root/sys.user.accounts
			// but $_id is set to owner's name instead of 'account' 
			foreach($documents as $document) {
				if(isset($document->_id) && $document->_id=='account') {
					// we're modifying object, so make a copy
					$newDocument=new \sacvoyage\Object($document); 
					$newDocument->_id=$collection->getOwner()->getName();
				}
			}
			
			self::$catch_root_write=true;
		} 
		
		// root adds share-config to user account
		elseif(self::$catch_root_write && $collection->getOwner()->getName()=='root' 
		&& $collection->getName()=='sys.user.share') {
			self::$catch_user_write=false;
			$domain=$collection->getOwner()->getDomain();
			foreach($documents as $document) {
				if(isset($document->owner)) {
					// we're modifying object, so make a copy
					$newDocument=new \sacvoyage\Object($document);
					$newDocument->_id='account';
					unset($newDocument->owner);
					
					$owner=$domain->loadUser($document->owner);
					$ownCollection=$owner->getProperty()->getCollection('sys.cfg');
					$ownCollection->write($document);
				}
			}
			self::$catch_user_write=true;
		}
		
		return true;
	}
	
	public function onDelete($collection, $query) {
		
		// user deletes own share-config from own sacvoyage -> delete own account
		if(self::$catch_user_delete && $collection->getName()=='sys.cfg') {
			self::$catch_root_delete=false;
			
			$owner=$collection->getOwner();
			$root=$collection->getOwner()->getDomain()->loadUser('root');
			$allAccounts=$root->getProperty()->getCollection('sys.user.accounts');
			$allAccounts->delete();
				
			$newQuery=new Object($query);
			$newQuery->select=new Object(array('_id'=>true, '_rev'=>true));
			
			$q=new Object();
			$q->filter=new Object();
			$q->filter->_id=$owner->getName();
			foreach($collection->find($newQuery) as $document) {
				if($document->_id=='account') {
					if(isset($query->status) && ($query->status=='all' || $query->status=='stale')) {
						$q->filter->_rev=$document->_rev;
					}
					$allAccounts->delete($query);
				}
			}
			
			self::$catch_root_delete=true;
		}
		
		// root adds share-config to user account
		elseif(self::$catch_root_delete && $collection->getOwner()->getName()=='root' 
		&& $collection->getName()=='sys.user.share') {
			self::$catch_user_delete=false;

			$newQuery=new Object($query);
			$newQuery->select=new Object(array('_id'=>true, '_rev'=>true));
			
			$q=new Object();
			$q->filter=new Object();
			$q->filter->_id='config';
			foreach($collection->find($newQuery) as $document) {
				if(isset($document->_id)) {
					$owner=$collection->getDomain()->loadUser($document->_id);
					$ownerCfg=$owner->getProperty()->getCollection('sys.cfg');
					
					if(isset($query->status) && ($query->status=='all' || $query->status=='stale')) {
						$q->filter->_rev=$document->_rev;
					}
					
					$ownerCollection->delete($q);
				}
			}
			self::$catch_user_delete=true;
		}
		return true;
	}
	
	private static $catch_user_write=true;
	private static $catch_root_write=true;
	
	private static $catch_user_delete=true;
	private static $catch_root_delete=true;
	
}



?>